package edu.swust.graduation_project.eauction.fragment

import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import edu.swust.graduation_project.eauction.Adapter.DeliveryListAdapter
import edu.swust.graduation_project.eauction.Bean.OrderBean
import edu.swust.graduation_project.eauction.R
import edu.swust.graduation_project.eauction.activity.OrderActivity
import edu.swust.graduation_project.eauction.activity.OrderEditActivity
import java.sql.Connection
import java.sql.DriverManager
import java.sql.SQLException
import java.util.*


class BuyerFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view: View = inflater.inflate(R.layout.fragment_buyer, container, false)
        return view
    }

    override fun onStart() {
        super.onStart()
        val mSharedPreferences: SharedPreferences =
            requireContext().getSharedPreferences(
                "user",
                AppCompatActivity.MODE_PRIVATE
            )
        val userName = mSharedPreferences.getString("username", "")
        var list: ArrayList<OrderBean> = MysqlTobean(userName)
        var searchResult = ArrayList<OrderBean>(list)
        val mSvBuyer=view?.findViewById<SearchView>(R.id.sv_buyer)
        val mLvBuyer=view?.findViewById<ListView>(R.id.lv_buyer)
        val myAdapter = DeliveryListAdapter(requireContext(), searchResult)

        mLvBuyer?.adapter=myAdapter

        mLvBuyer?.setOnItemClickListener { adapterView, view, i, l ->
            val intent = Intent(context, OrderActivity::class.java)
            intent.putExtra("buyer", searchResult[i].buyer)
            intent.putExtra("item_create_time", searchResult[i].item_create_time)
            intent.putExtra("buyerPhone", searchResult[i].buyerPhone)
            intent.putExtra("item_name", searchResult[i].item_name)
            intent.putExtra("buyer_address", searchResult[i].buyer_address)
            intent.putExtra("seller", searchResult[i].seller)
            intent.putExtra("sellerPhone", searchResult[i].sellerPhone)
            intent.putExtra("seller_express_number",searchResult[i].seller_express_number)
            intent.putExtra("isBuy",true)
            startActivity(intent)
        }

        mSvBuyer?.isIconifiedByDefault = false
        mSvBuyer?.isSubmitButtonEnabled = true
        mSvBuyer?.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                searchResult.clear()
                for (value in list) {
                    if (value.item_name.contains(newText!!)) {
                        searchResult.add(value)
                    }
                }
                myAdapter.notifyDataSetChanged()
                return true
            }
        }
        )

    }

   private fun MysqlTobean(userName: String?):ArrayList<OrderBean>{
        var orderBeanList = ArrayList<OrderBean>()
        val thread = Thread {
            try {
                val sqlSelect = "SELECT * FROM `Delivery` WHERE `buyer` = '$userName'"
                //建立连接
                val cn: Connection = DriverManager.getConnection(
                    "*",
                    "*", "*"
                )
                val ps = cn.createStatement()
                val resultSet = ps!!.executeQuery(sqlSelect)
                if (resultSet != null) {
                    while (resultSet.next()) {
                        val item_create_time = resultSet.getTimestamp("item_create_time")
                        val item_name=resultSet.getString("item_name")
                        val buyer = resultSet.getString("buyer")
                        val seller = resultSet.getString("seller")
                        val seller_express_number = resultSet.getString("seller_express_number")
                        val buyer_address = resultSet.getString("buyer_address")
                        val buyerPhone = resultSet.getString("buyer_Phone")
                        val sellerPhone = resultSet.getString("seller_Phone")
                        orderBeanList.add(
                            OrderBean(
                                item_create_time,
                                item_name,
                                buyer,
                                seller,
                                seller_express_number,
                                buyer_address,
                                buyerPhone,
                                sellerPhone
                            )
                        )
                    }
                    Log.d("itemBeanList",orderBeanList.toString())
                }
                cn.close()
                ps.close()
                resultSet.close()
            } catch (e: ClassNotFoundException) {
                e.printStackTrace()
            } catch (e: SQLException) {
                e.printStackTrace()
            }
        }
        thread.start()
        thread.join()
        if (orderBeanList.isEmpty()) Thread.sleep(10)
        return orderBeanList
    }

}

